<?php

namespace spider;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use think\Exception;

/**
 * 采集网站：https://www.etsy.com/
 */
class Etsy
{
    protected static $host = 'https://www.etsy.com/';


    public static function spider($params)
    {
        if (str_contains($params['get_url'], 'search?q=') || str_contains($params['get_url'], 'query=')) {
            $params['page'] = $params['page'] ?? 1; //默认第一页
            $ret = []; //抓取到的数据集合
            self::spiderSearchList($params, $ret);
            $ret = [
                'list' => $ret,
                'count' => count($ret),
//                'spider_type' => 'list',
            ];

            $issetNameTempArr = [];
            foreach ($ret['list'] as &$row) {
                $title = str_replace(["/", "*", "\\", "?", "\"", "<", ">", "|", ":", ","], '', $row['name']);
                $tempTitle = strtolower($title);//win下不区分大小写，linux区分
                // 本地保存路径
                $path = "/shop/images/teepublic/$title.jpg";

                //判断是否需要重命名
                if (in_array($tempTitle, $issetNameTempArr) || file_exists(ROOT_PATH . "public" . $path)) {
                    $title .= "--" . uniqid();
                    $path = "/shop/images/teepublic/$title.jpg";
                }
                $issetNameTempArr[] = $tempTitle;
                $row['local_img_url'] = $path; //记录本地地址

                $temp = $row;
                $temp['platform'] = 'teepublic'; //记录平台

                addSaveImagesQueue($temp);
            }
        }
        else {
            //单个链接抓取
            $ret = self::spiderSingle($params);
//            $ret['spider_type'] = 'single';
            if (empty($ret['shopInfo'])) return false;

            //加入保存图片到本地队列
            $title = str_replace(["/", "*", "\\", "?", "\"", "<", ">", "|", ":", ","], '', $ret['shopInfo']['name']);
            $tempTitle = $title . "--" . uniqid();
            $path = "/shop/images/teepublic/$tempTitle.jpg";

            $pathArr = [];
            // 本地保存路径
            foreach ($ret['list'] as &$row) {
                $temp = $row;
                $temp['platform'] = 'teepublic'; //记录平台
                $tempImage = explode("_____", $row['images']);
                $pathTempArr = [];
                foreach ($tempImage as $key => $image) {
                    $temp['images'] = $image;
                    //加入保存图片到本地队列
                    if ($temp['is_saveimg']) {
                        $tempTitle = $title . "--" . uniqid();
                        $path = "/shop/images/teepublic/$tempTitle.jpg";
                        $pathArr[$key] = $path;
                        $temp['local_img_url'] = $path;

                        self::addSaveImagesQueue($temp);
                    } else {
                        $path = $pathArr[$key] ?? $path;
                    }
                    $pathTempArr[] = $path;
                }
                $row['local_img_url'] = implode(',', $pathTempArr); //记录本地地址

                unset($row['is_saveimg']);
            }
        }
        return $ret;
    }


    private static function spiderSearchList($params, &$data)
    {
        //查询页数大于1时，每页暂停1秒，以免被封，如果后续有IP池可不用
        if ($params['page'] > 1) sleep(1);

        //请求URL的数据
        $url = $params['get_url'] . "&page=" . $params['page'];
        echo $url . "\n";

        try {
            $html = self::request($url);
            /*TODO：调试时写入文件，查看页面代码用*/
            file_put_contents(SPIDER_HTML_PATH . "/etsy/page_{$params['page']}.html", $html);
//            $html = file_get_contents(SPIDER_HTML_PATH . "/teepublic{$params['page']}.html");

            $namePattern = '/<div[^>]*class="[^"]*tp-design-tile jsDesignContainer[^"]*"[^>]*>(.*?)\s+<\/p>\s+<\/div>\s+<\/div>/s';
            preg_match_all($namePattern, $html, $nameMatches);

            // 定义标题正则表达式
            $titlePattern = "/<h2 class='tp-design-tile__title ellip'>(.*?)<\/h2>/";
            // 定义价格正则表达式
            $pricePattern = "/<span class='tp-design-tile__price--regular'>(.*?)<\/span>/s";
            // 定义图片正则表达式
            $imagePattern = '/<img class="tp-design-tile__image".*?src="(.*?)"/s';
            // 定义链接正则表达式
            $linkPattern = '/<a[^>]*href="([^"]*)"[^>]*class="[^"]*tp-design-image__preview_link[^"]*"[^>]*>/';

            foreach ($nameMatches[1] as $row) {
                preg_match($titlePattern, $row, $titleMatches);
                $productName = isset($titleMatches[1]) ? trim($titleMatches[1]) : '';

                preg_match($pricePattern, $row, $priceMatches);
                $productPrice = isset($priceMatches[1]) ? trim($priceMatches[1]) : '';

                preg_match($imagePattern, $row, $imageMatches);
                $productImage = isset($imageMatches[1]) ? trim($imageMatches[1]) : '';

                preg_match($linkPattern, $row, $linkMatches);
                $productLink = isset($linkMatches[1]) ? trim($linkMatches[1]) : '';

                $tempNewData = [
                    'shop_uniqid' => \fast\Random::alnum(32),
                    'task_id' => (int)$params['task_id'],
                    'name' => $productName,
                    'price' => $productPrice,
                    'images' => $productImage,
                    'url' => self::$host . $productLink,
                ];
                $data[] = $tempNewData;

                //将单个商品放入队列抓取
                $tempNewData['get_url'] = $tempNewData['url'];
                addJob('app\\jobs\\Task', $tempNewData, 'task');
            }
            $nextPage = self::getNextPageIndex($html);
            if ($nextPage) {
                //调用抓取
                $params['page'] = $nextPage;
//                $params['get_url'] = $params['get_url'] . "&page=" . $nextPage;
                addJob('app\\jobs\\Task', $params, 'task');
//                self::spiderSearchList($params, $data);
            }

            echo 1;
            return;
        } catch (Exception $e) {
            echo "Error: " . $e->getMessage();
        }
    }



}